<!--微服务新增-->
<template>
    <a-modal v-model="showAddTable" :width="640"
             :title="title ? '新增微服务' : '编辑微服务'"
             :maskClosable="false"
             :destroyOnClose="true" @cancel="cancelApply">
        <a-form :form="form" @keydown.native.enter.prevent="saveApply" class="modelSearch">
            <a-form-item label="微服务标识：" v-bind="formStyle" required>
                <a-input placeholder="请输入微服务名" disabled
                         v-decorator="['code']"
                />
            </a-form-item>
            <a-form-item label="微服务名：" v-bind="formStyle" required>
                <a-input placeholder="请输入微服务名"
                         v-decorator="[
                        'name',
                        {
                            rules: [
                                { required: true, message: '请输入汉字、字母、数字、_或-字符' },
                                { validator: this.verify.validateName.bind(this) }
                            ],
                            getValueFromEvent: event => {
                                return this.verify.formalSpace(event)
                            }
                        }
                    ]"
                />
            </a-form-item>
        </a-form>
        <div slot="footer">
            <a-button type="default" size="default" @click="cancelApply()">取消</a-button>
            <a-button type="primary" size="default" @click="saveApply()">确定</a-button>
        </div>
    </a-modal>
</template>

<script>
    import verify from 'libs/verify'
    export default {
        name: 'addEdit',
        data () {
            return {
                verify,
                form: this.$form.createForm (this),
                showAddTable: false,
                id: 0,
                title:true
            }
        },
        computed: {
            formStyle () {
                return this.$store.state.common.formStyle
            },
        },
        methods: {
            /**
             * Function    : saveApply
             * Description : 打开弹窗
             * Input       : name
             * Return      : none
             */
            showModal () {
                this.showAddTable = true
                this.title = true
            },
            /**
             * Function    : showEdit
             * Description : 编辑时单位显示
             * Input       : opt单个单位所有数据
             * Return      : none
             */
            showEdit ( opt ) {
                this.showAddTable = true
                this.title = false
                this.id = opt.id
                this.$nextTick (() => {
                    this.form.setFieldsValue (this.$pick (opt, [ 'name', 'code' ]))
                })
            },

            /**
             * Function    : saveApply
             * Description : 用户信息新增、编辑保存
             * Input       : name
             * Return      : none
             */
            saveApply () {
                let _this = this
                this.form.validateFields (( err, value ) => {
                    if (!err) {
                        let url = 'group/saveGroup'
                        if (!this.title) {
                            value.id = this.id
                            url = 'sysserver/updSysServer'
                        }
                        let para = Object.assign ({}, value)
                        _this.$parent.$parent.spinning = true
                        _this.$post(url, Object.assign(para)).then(res => {
                            if (res.code === 0) {
                                _this.showAddTable = false
                                this.$emit ('init')
                                this.$message.success (res.message)
                                _this.id = 0
                                _this.cancelApply()
                            } else {
                                _this.$message.error(res.message)
                            }
                            _this.$parent.$parent.spinning = false
                        }).catch((e) => {
                            _this.$parent.$parent.spinning = false
                        })
                    }
                })
            },

            /**
             * Function    : cancelApply
             * Description : 取消弹出框
             * Input       : name
             * Return      : none
             */
            cancelApply () {
                this.showAddTable = false
                let _this = this
                setTimeout (function () {
                    _this.form.resetFields ()
                }, 100)
            },
        }
    }
</script>
